package com.yin.myproject.thread.spinlock;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Eason
 * @create 2018-03-07 9:33
 **/
public class SpinLock implements Lock {

    private AtomicReference<Thread> sign = new AtomicReference<>();

    @Override
    public void lock() {
        Thread current = Thread.currentThread();
        while (!sign.compareAndSet(null, current)) {

        }
    }

    @Override
    public void unlock() {
        Thread current = Thread.currentThread();
        sign.compareAndSet(current, null);
    }
}
